package kr.hkit.powerjava.chther25.chat;

public class QuizeProtocol {
	private static final int WATTING = 0;
	private static final int PROBLEM = 1;
	private static final int ANSWER = 2;

	private static final int NUMPROBLEMS = 3; // 문제수

	private int state = WATTING; // 현재상태
	private int currentProblem = 0; // 현재 문제번호

	private String[] problems = { "네트워크 처리 패키지는", "자바의 최신버전은?",
			"인터넷에서 컴퓨터를 식별하는주소는?" };
	private String[] answers = { "java.io", "1.7", "IP 주소" };

	public String process(String theInput) {
		String theOutput = null;
		switch (state) {

			case QuizeProtocol.WATTING:
				theOutput = "퀴즈를 시작합니다(y/n)";
				state = QuizeProtocol.PROBLEM;
				break;
			case QuizeProtocol.PROBLEM:
				if(theInput.equalsIgnoreCase("y")){
					theOutput = problems[currentProblem];
					state = QuizeProtocol.ANSWER;
				}else{
					state = QuizeProtocol.WATTING;
					theOutput = "quit";
				}
				break;
			case QuizeProtocol.ANSWER:
				if(theInput.equalsIgnoreCase(answers[currentProblem])){
					theOutput ="정답입니다. 계속하시겠습니까?(y/n)";
					state =QuizeProtocol.PROBLEM;
				}else{
					theOutput ="오답입니다. 계속하시겠습니까(y/n)";
					state =PROBLEM;
				}
				currentProblem = (currentProblem+1)%NUMPROBLEMS;
				break;

		}
		return theOutput;
	}

}
